/*
 * @Author: jianglijun 
 * @Date: 2019-04-20 21:29:12 
 * @Last Modified by: jianglijun
 * @Last Modified time: 2019-04-21 05:19:02
 */
 //      CONTROLLERTS
 let path = require("path");
let { addMusicdb } = require("../models/music.js");
let { isContained  } = require("../utils/utils.js");
module.exports = {
    
    /************************** 增加音乐信息  *******************/
    async addMusic(ctx,next){
        let {title,singer,time,uid,type} = ctx.request.body;
        if( (!title) || (!singer) ||(!time) ||(!uid) ){
            ctx.body= {
                errno:4,
                mes:"标题,歌手,时间,uid不能为空"
            }
            return;
        }
        let {file,filelrc} = ctx.request.files;
        if(!file){
            ctx.body= {
                errno:4,
                mes:"歌曲不能为空"
            }
            return
        }
        let q1 = [...Object.keys(ctx.request.body),...Object.keys(ctx.request.files)];
        if(q1.length<4){
            ctx.body= {
                errno:5,
                mes:"参数不正确1"
            }
            return;
        }
        let flag = isContained(["title","singer","time","file","filelrc","type","uid"],q1,false);
        if(!flag){
            ctx.body= {
                errno:4,
                mes:"参数不正确2"
            }
            return;
        }
        let paramObj = {
            title,
            singer,
            time:parseInt(time+""),
            file:"/public/files/"+path.parse(file.path).base,
        }
        paramObj.filelrc = null;
        paramObj.type = 1;
        paramObj.uid = +uid;
        // console.log(filelrc.path);
        if(filelrc){
            paramObj.filelrc  = "/public/files/"+path.parse(filelrc.path).base
        }
        try {
            let { affectedRows, insertId } = await addMusicdb(paramObj);
            if(affectedRows>0){
                ctx.body = {
                    errno:0,
                    msg:"插入成功"
                }
            }else {
                ctx.body = {
                    errno:10,
                    msg:"插入失败"
                }
            }
        } catch (error) {
            ctx.throw(error);
        }
    }
}